<?php

require_once ('Zend/Controller/Router/Route/Regex.php');

class Rest_Route_Regex extends Zend_Controller_Router_Route_Regex
{

	protected $_method;

	protected static $_validMethods = array('GET' , 'POST' , 'PUT' , 'DELETE' , 'HEAD' , 'OPTIONS');

	/**
	 * Instantiates route based on passed Zend_Config structure
	 *
	 * @param Zend_Config $config Configuration object
	 */
	public static function getInstance(Zend_Config $config)
	{
		$defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
		$map = ($config->map instanceof Zend_Config) ? $config->map->toArray() : array();
		$reverse = (isset($config->reverse)) ? $config->reverse : null;
		$method = ($config->method) ? $config->method : 'GET';
		return new self($config->route, $defs, $map, $reverse, $method);
	}

	public function __construct($route, $defaults = array(), $map = array(), $reverse = null, $method = 'GET')
	{
		parent::__construct($route, $defaults, $map, $reverse);
		$this->_method = $method;
	}

	/**
	 * Matches a user submitted path with parts defined by a map. Assigns and
	 * returns an array of variables on a successful match.
	 *
	 * @param string $path Path used to match against this routing map
	 * @return array|false An array of assigned values or a false on a mismatch
	 */
	public function match($path)
	{
		Rest_Controller::startRest();
		$request = Zend_Controller_Front::getInstance()->getRequest();
		$method = $request->getMethod();
		
		if ($method != $this->_method)
			return false;
		else
			return parent::match($path);
	}

}
